package cn.store.web.servlet;

import cn.store.domain.Category;
import cn.store.domain.PageModel;
import cn.store.domain.Product;
import cn.store.service.CategoryService;
import cn.store.service.ProductService;
import cn.store.service.serviceImp.CategoryServiceImp;
import cn.store.service.serviceImp.ProductServiceImp;
import cn.store.utils.JedisUtils;
import cn.store.web.base.BaseServlet;
import com.alibaba.fastjson.JSON;
import net.sf.json.JSONArray;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ProductServlet extends BaseServlet {
	public String findProductByPid(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//获取商品pid
		String pid=request.getParameter("pid");
		//根据商品pid查询商品信息
		ProductService ProductService=new ProductServiceImp();
		Product product=ProductService.findProductByPid(pid);
		//将获取到的商品放入request
		request.setAttribute("product", product);
		//转发到/jsp/product_info.jsp
		return "/jsp/product_info.jsp";
	}
	
	//暂时没用
	public String findProductsByCidWithPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		//获取cid,num
		String cid=request.getParameter("cid");
		int curNum=Integer.parseInt(request.getParameter("num"));
		//调用业务层功能:以分页形式查询当前类别下商品信息
		//返回PageModel对象(1_当前页商品信息2_分页3_url)
		ProductService ProductService=new ProductServiceImp();
		PageModel pm=ProductService.findProductsByCidWithPage(cid,curNum);
		//将PageModel对象放入request
		request.setAttribute("page", pm);
		//转发到/jsp/prod
		return  "/jsp/product_list.jsp";
	}

	//分页+搜索
	public String findAllProductsWithPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		//获取当前页
		int curNum;
		if(req.getParameter("num") ==null){
			curNum = 1;
		}else{
			curNum = Integer.parseInt(req.getParameter("num"));
		}
		//获取pname搜索词
		String pname = req.getParameter("pname");
		String cid = req.getParameter("cid");
		//封装搜索词
		Map<String,Object> searchWords = new HashMap<>();
		searchWords.put("pname",pname);
		searchWords.put("cid",cid);
		//调用业务层查全部商品信息返回PageModel
		ProductService ProductService=new ProductServiceImp();
		PageModel pm=ProductService.findAllProductsWithPage(curNum,searchWords);
		pm.setUrl("ProductServlet?method=findAllProductsWithPage");
		//将PageModel放入request
		req.setAttribute("page", pm);

		//包装回搜索词,防止页面跳转丢失
		Product searchProduct = new Product();
		searchProduct.setPname(pname);
		searchProduct.setCid(cid);
		req.setAttribute("searchProduct", searchProduct);
		/*添加类目列表*/
		findAllCats(req, resp);
		//转发到/admin/product/list.jsp
		return "/jsp/product_list.jsp";
	}
	//带Redis的Catagory查询,设置到request的Attribute中
	public void findAllCats(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		//在redis中获取全部分类信息
		Jedis jedis;
		List<Category> allCats;
		try {
			jedis = JedisUtils.getJedis();
			String jsonStr = jedis.get("allCats");
			if (null == jsonStr || "".equals(jsonStr)) {
				//调用业务层获取全部分类
				CategoryService CategoryService = new CategoryServiceImp();
				allCats = CategoryService.getAllCats();
				//将全部分类转换为JSON格式的数据
				jsonStr = JSONArray.fromObject(allCats).toString();
				System.out.println(jsonStr);
				//将获取到的JSON格式的数据存入redis
				jedis.set("allCats", jsonStr);
				System.out.println("redis缓存中没有数据");
				
			} else {
				System.out.println("redis缓存中有数据");
				//将全部分类信息响应到客户端
				allCats = JSON.parseArray(jsonStr,Category.class);
			}

			JedisUtils.closeJedis(jedis);
		}catch (Exception e){
			System.out.println("Redis出错");
			e.printStackTrace();
			/*正常业务不受影响*/
			CategoryService CategoryService = new CategoryServiceImp();
			allCats = CategoryService.getAllCats();
		}
		//将全部分类信息响应到客户端
		req.setAttribute("allCats", allCats);

	}
}
